<?php
require_once(dirname(__FILE__) . '/include/config.inc.php');


$dosql->Execute("SELECT * FROM  `fa_cms_archives`  WHERE `model_id`=2   ORDER BY id asc", 8);
while ($row3 = $dosql->GetArray(8)) {
    $aid = $row3['tbid'];
    $id = $row3['id'];
    $url = "http://www.mysd168.com/api/goods/list?from=0&limit=88&authorid=" . $aid;

    $res = httpGet($url);
    if (preg_match('/^\xEF\xBB\xBF/', $res)) $res = substr($res, 3);

    $arr = json_decode($res, true);


    if($arr['data']['list']) {
        foreach ($arr['data']['list'] as $key => $val) {
            var_dump($val);

            $title=$val['name'];

            $time=time();
            $tbid=$val['goodid'];
            $capacity=$val['rongliang'];
            $num=$val['bianma'];
            $image="";
            if($val['slimg']!=""){
                $img=$val['slimg'];
                $p_arr=parse_url($img);
                $image=$p_arr['path'];
            }

            $niliao=$val['niliao'];
            $channel_id=getClassidFromName($niliao);

            $dosql->Execute("insert into `fa_cms_archives`(channel_id,model_id,title,image,createtime,updatetime,publishtime,status,tbid) values ('$channel_id','3','$title','$image','$time','$time','$time','normal','$tbid')");
            $lastid=$dosql->GetLastID();
            $dosql->ExecNoneQuery("insert into `fa_cms_addonproduct`(`id`,`content`,`capacity`,`num`,`aid`) values ('$lastid','','$capacity','$num','$id')");

            if($img!=""){
                $aa=GetRemPic($img);
            }

            echo $title."添加成功"."<br>";

        }
    }
}


function getClassidFromName($name){
    global $dosql;
    if($name!=""){
        $m=$dosql->GetOne("SELECT `id` FROM `fa_cms_channel` WHERE `name`='$name' ");
        if(is_array($m) && isset($m['id']) ){
            return  $m['id'];
        }
    }
}


function httpGet($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    //curl_setopt($curl,CURLOPT_CAINFO,dirname(__FILE__).'/cacert.pem');//这是根据http://curl.haxx.se/ca/cacert.pem 下载的证书，添加这句话之后就运行正常了
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);

    curl_close($curl);

    return $res;
}


/*
 * 获取一个远程图片
 *
 * @access  public
 * @param   $url       string  获取字段所属模型
 * @return  $save_url  string  返回上传后地址
*/
function GetRemPic($url)
{

    $filename_arr = parse_url($url);
    $path = $filename_arr['path'];
    $filename_arr = explode('/', $path);
    $filename = array_pop($filename_arr);

    $upload_url = '/uploads/goods/';
    $upload_dir = PHPMYWIND_UPLOAD . "/goods/";

    //上传文件路径
    $save_url = $upload_url . $filename;
    $save_dir = $upload_dir . $filename;
    echo "保存路径" . $save_dir . "<br/>";

    if (file_exists($save_dir)) {
        return $save_url;
    }


    //引入下载类
    require_once(PHPMYWIND_DATA . '/httpfile/down.class.php');

    //初始化变量
    $htd = new HttpDown();
    $htd->OpenUrl($url);


    //判断文件类型
    $sparr = array('image/pjpeg', 'image/jpeg', 'image/gif', 'image/png', 'image/xpng', 'image/wbmp');
    if (!in_array($htd->GetHead("content-type"), $sparr)) {
        return FALSE;
    } else {
        $type = $htd->GetHead("content-type");

        if ($type == 'image/gif')
            $tempfile_ext = 'gif';

        else if ($type == 'image/png')
            $tempfile_ext = 'png';

        else if ($type == 'image/wbmp')
            $tempfile_ext = 'bmp';

        else
            $tempfile_ext = 'jpg';


        echo "保存";

        $rs = $htd->SaveToBin($save_dir);
    }

    $htd->Close();
    return ($rs ? $save_url : '');
}


?>